<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Access extends M_Controller {

	function __construct ()
	{
		parent::__construct();
		$this->load->model('Accessmodel');
		$this->load->library('msmarty');
		$this->load->library('mjavascript');
		$this->load->helper('form');
		$this->load->helper('url');
		$this->mjavascript->setPage($this->uri->uri_string());
//		$this->Accessmodel->id = (int)$this->session->userdata('user_id');
//		$this->logged = $this->Usermodel->get();
	}

	function user ($user_id) {
		$content = '';
		if (empty($user_id)) {
			$this->msmarty->assign('message', 'You should specify user!');
			$content = $this->msmarty->fetch('error.html');
		} else {
			$this->Accessmodel->user_id = (int)$user_id;
			if ($this->input->post('submit')) {
				$this->Accessmodel->groups = $this->input->post('group');
				$this->Accessmodel->pages = $this->input->post('pages');
				if ($this->Accessmodel->saveUserPermissions()) {
					$this->msmarty->assign('message', 'User permissions saved.');
					$content = $this->msmarty->fetch('success.html');
				} else {
					$this->msmarty->assign('message', 'An error occurred while trying to save user permissions.');
					$content = $this->msmarty->fetch('error.html');
				}
			} else {
				$groupList = $this->Accessmodel->getGroups();
				$pagesList = $this->Accessmodel->getPages();

				$controls = '';

				foreach ($groupList as $group) {
					$this->msmarty->assign('cname', 'group['.$group['id'].']');
					$this->msmarty->assign('cvalue', $group['user_id']);
					$this->msmarty->assign('ccomment', $group['name']);
					$controls .= $this->msmarty->fetch('config_checkbox.html');
				}
				$options = '';
				var_dump($pagesList);
				foreach ($pagesList as $page) {
					var_dump($page);
					$this->msmarty->assign('cname', $page['id']);
					$this->msmarty->assign('cvalue', $page['user_id']);
					$this->msmarty->assign('ccomment', $page['page']);
					$options .= $this->msmarty->fetch('control_option.html');
				}
				$this->msmarty->assign('options', $options);
				$this->msmarty->assign('cname', 'pages');
				$this->msmarty->assign('ccomment', 'User access pages');
				$controls .= $this->msmarty->fetch('control_select.html');

				$this->msmarty->assign('controls', $controls);
				$this->msmarty->assign('action', site_url($this->uri->uri_string()));
				$content .= $this->msmarty->fetch('form.html');
			}
		}
		$this->msmarty->assign('content', $content);
		$this->msmarty->display('main.html');
	}

	function group ($group_id = null) {
		$content = '';
		$this->Accessmodel->group_id = $group_id;
		//TODO creating new group stuff here

		if ($this->input->post('submit') && $this->input->post('name')) {
			//save access group stuff

			$this->Accessmodel->group['name'] = $this->input->post('name');

			$pages = $this->input->post('pages');
			if (!empty($pages)) { $this->Accessmodel->group['pages'] = is_array($pages) ? $pages : array($pages); }
			else { $this->Accessmodel->group['pages'] = array(); }

			$users = $this->input->post('users');
			if (!empty($users)) { $this->Accessmodel->group['users'] = is_array($users) ? $users : array($users); }
			else { $this->Accessmodel->group['users'] = array(); }

			if ($this->Accessmodel->saveGroup()) {
				$this->msmarty->assign('message', 'Access group saved.');
				$content = $this->msmarty->fetch('success.html');
			} else {
				$this->msmarty->assign('message', 'An error occurred while trying to save access group.');
				$content = $this->msmarty->fetch('error.html');
			}
		} else {
			$content .= $this->editGroupForm();
			$data = $this->mjavascript->collect();
		}
		$this->msmarty->assign('content', $content);
		$this->msmarty->assign('jslibs', $this->mjavascript->getCode());
		$this->msmarty->display('main.html');
	}

	function editGroupForm ($html = true) {
		if ($this->Accessmodel->get()) {
			$ccomment = $this->Accessmodel->group['name'];
			$groupPages = $this->Accessmodel->group['pages'];
			$groupUsers = $this->Accessmodel->group['users'];
		} else {
			$ccomment = null;
			$groupPages = array();
			$groupUsers = array();
		}
		$pagesList = $this->Accessmodel->getPages();
		$usersList = $this->Accessmodel->getUsers();
		$controls = '';

		$pages = array_intersect($pagesList, $groupPages);
		$users = array_intersect($usersList, $groupUsers);

		if ($html) {
			$comment = form_input(array(
				'name'        => 'name',
				'id'          => 'name',
				'value'       => $ccomment,
				'maxlength'   => '50',
				'size'        => '20',
			));
			$controls .= $this->load->view('1row2cell', array('first' => 'name', 'second' => $comment), true);

			$options = array();
			$selected = array();
			foreach ($pagesList as $key => $page) {
				$options[$page->id] = $page->page;
				if (array_key_exists($key, $pages)) { $selected[$page->id] = $page->page; }
			}
			$pagesControl = form_dropdown('pages', $options, $selected);
			$controls .= $this->load->view('1row2cell', array('first' => 'Access group pages', 'second' => $pagesControl), true);

			$options = array();
			$selected = array();
			foreach ($usersList as $key => $user) {
				$options[$user->id] = $user->login;
				if (array_key_exists($key, $users)) { $selected[$user->id] = $user->login; }
			}
			$usersControl = form_dropdown('users', $options, $selected);
			$controls .= $this->load->view('1row2cell', array('first' => 'Access group users', 'second' => $usersControl), true);

			$send = form_button(array(
				'name' => 'send',
				'id' => 'send',
				'value' => 'true',
				'type' => 'button',
				'content' => 'Send'
			));
			$controls .= $this->load->view('1row1cell', array('content' => $send), true);

			return $this->load->view('newaccessgroup', array('controls' => $controls), true);
		}
	}

	function savegroup () {
		fb($_POST);
		$name = $this->input->post('name', true);
		$pages = $this->input->post('pages', true);
		$users = $this->input->post('users', true);

		if ($name) {
			$this->Accessmodel->group['name'] = $name;
			$groupPages = $this->Accessmodel->group['pages'] = $pages;
			$groupUsers = $this->Accessmodel->group['users'] = $users;
			if ($this->Accessmodel->saveGroup()) { return false; }
		}
	}

	function page ($page_id = null) {
		$content = '';
		$this->Accessmodel->page_id = $page_id;
		//TODO creating new group stuff here

		if ($this->input->post('submit')) {
			//save access group stuff
			$this->Accessmodel->group['page'] = $this->input->post('page');
			$this->Accessmodel->group['groups'] = $this->input->post('groups');
			$this->Accessmodel->group['users'] = $this->input->post('users');
			if ($this->Accessmodel->savePage()) {
				$this->msmarty->assign('message', 'Access group saved.');
				$content = $this->msmarty->fetch('success.html');
			} else {
				$this->msmarty->assign('message', 'An error occurred while trying to save access group.');
				$content = $this->msmarty->fetch('error.html');
			}
		} else {
			if ($this->Accessmodel->get()) {
				$ccomment = $this->Accessmodel->group['name'];
				$groupPages = $this->Accessmodel->group['pages'];
				$groupUsers = $this->Accessmodel->group['users'];
			} else {
				$ccomment = null;
				$groupPages = array();
				$groupUsers = array();
			}
			$pagesList = $this->Accessmodel->getPages();
			$usersList = $this->Accessmodel->getUsers();
			$controls = '';

			$pages = array_intersect($pagesList, $groupPages);
			$users = array_intersect($usersList, $groupUsers);

			$this->msmarty->assign('cname', 'name');
			$this->msmarty->assign('ccomment', 'Group name');
			$this->msmarty->assign('cvalue', $ccomment);
			$comment = $this->msmarty->fetch('control_text.html');
			$controls .= $comment;

			$options = '';
			foreach ($pagesList as $key => $page) {
				$this->msmarty->assign('cname', $page->id);
				$this->msmarty->assign('ccomment', $page->page);
				$this->msmarty->assign('cvalue', in_array($page, $pages));
				$options .= $this->msmarty->fetch('control_option.html');
			}

			$this->msmarty->assign('options', $options);
			$this->msmarty->assign('cname', 'pages');
			$this->msmarty->assign('ccomment', 'Access group pages');
			$controls .= $this->msmarty->fetch('control_multyselect.html');

			$options = '';
			foreach ($usersList as $key => $user) {
				$this->msmarty->assign('cname', $user->id);
				$this->msmarty->assign('ccomment', $user->login);
				$this->msmarty->assign('cvalue', in_array($user, $users));
				$options .= $this->msmarty->fetch('control_option.html');
			}

			$this->msmarty->assign('options', $options);
			$this->msmarty->assign('cname', 'users');
			$this->msmarty->assign('ccomment', 'Access group users');
			$controls .= $this->msmarty->fetch('control_multyselect.html');

			$this->msmarty->assign('cname', 'submit');
			$this->msmarty->assign('ccomment', 'OK');
			$controls .= $this->msmarty->fetch('control_submit.html');

			$this->msmarty->assign('controls', $controls);
			$this->msmarty->assign('enctype', '');
			$this->msmarty->assign('action', site_url($this->uri->uri_string()));
			$content .= $this->msmarty->fetch('form.html');
		}
		$this->msmarty->assign('content', $content);
		$this->msmarty->assign('jslibs', $this->mjavascript->getCode());
		$this->msmarty->display('main.html');
	}

	function users () {

	}

	function groups () {

	}

	function pages () {

	}
}
